Integrating API Gateway with S3 for Secure File Uploads

Integrating API Gateway with S3 for Secure File Uploads

Clock Icon2024.08.19

Introduction

Hello, I'm Hemanth from the Alliance Department. In this blog, I will demonstrate how to use AWS API Gateway to upload files to an S3 bucket. This tutorial will guide you through the setup process, from creating the necessary AWS resources to configuring the API Gateway, enabling you to efficiently manage file uploads to S3 using HTTP requests.

API-S3.drawio

AWS

Amazon Web Services, or AWS, is a cloud service platform that provides content distribution, database storage, processing capacity, and other features to support corporate expansion. AWS has offered a broad range of services in many different categories, including Compute, Storage, Networking, Database, Management Tools, and Security.

S3

Simple and popular AWS Service for storage. Replicates data by default across multiple facilities. It charges per usage. It is deeply integrated with AWS Services. Buckets are logical storage units. Objects are data added to the bucket. S3 has a storage class on object level which can save money by moving less frequently accessed objects to a colder storage class.

API Gateway

AWS API Gateway is a fully managed service that enables developers to create, publish, maintain, monitor, and secure APIs at any scale. It acts as an entry point for applications to access back-end services, such as S3, securely and efficiently.

Demo

In the AWS Management Console, search for "S3" and select it. Click on "Create bucket".
Screenshot 2024-08-19 at 13.29.16
Provide a unique bucket name, keep other settings as default, and click "Create bucket". The bucket is successfully created.
Screenshot 2024-08-19 at 13.30.45
Screenshot 2024-08-19 at 13.32.17
Go to the IAM service, select "Roles," and click on "Create role".
Screenshot 2024-08-19 at 13.34.58
choose "API Gateway" as the service and click "Next".
Screenshot 2024-08-19 at 13.36.05
Click "Next" to proceed.
Screenshot 2024-08-19 at 13.37.56
Provide a role name and click "Create role".
Screenshot 2024-08-19 at 13.39.07
Screenshot 2024-08-19 at 13.39.41
The role is successfully created.
Screenshot 2024-08-19 at 13.41.41
Go to the role you've just created, navigate to the "Permissions" tab, and click on "Create inline policy".
Screenshot 2024-08-19 at 13.43.07
Choose the JSON editor, enter the following code, replacing the Resource section with your S3 bucket ARN, and click "Next":
Screenshot 2024-08-19 at 13.45.26
Provide a policy name and click "Create policy".
Screenshot 2024-08-19 at 13.47.29
The policy is successfully created and attached to the role.
Screenshot 2024-08-19 at 13.49.01
In the AWS Management Console, search for "API Gateway" and click on it. Click on "Create API"
Screenshot 2024-08-19 at 13.49.44
select "REST API" to build.
Screenshot 2024-08-19 at 13.51.01
Provide an API name and click "Create API".
Screenshot 2024-08-19 at 13.52.51
Click on "Create Resource".
Screenshot 2024-08-19 at 13.54.48
In the resource {bucket} and click "Create Resource".
Screenshot 2024-08-19 at 13.56.58
Name the resource {filename} and click "Create Resource".
Screenshot 2024-08-19 at 13.58.01
In the {filename} resource, create a method.
Screenshot 2024-08-19 at 13.59.19
Selecting type "PUT", set the Integration type to "AWS Service", select your preferred region, choose "S3" as the service, and select "PUT" as the HTTP method.
Screenshot 2024-08-19 at 14.03.51
Enter the path override for your S3 bucket, provide the ARN of the role created earlier in the execution role field, and click "Create Method".
Screenshot 2024-08-19 at 14.06.13
Go to the PUT method, click on "Integration Request" and then "Edit".
Screenshot 2024-08-19 at 14.08.12
Add the following mappings:
Name: bucket, Mapped from: method.request.path.bucket
Name: filename, Mapped from: method.request.path.filename
and click "Save".
Screenshot 2024-08-19 at 14.10.59
Click on "Deploy API".
Screenshot 2024-08-19 at 14.17.47
Screenshot 2024-08-19 at 14.21.06
Go to API settings, and in binary media types, manage the settings to allow any file types to be uploaded.
Screenshot 2024-08-19 at 14.22.40
Right now changing the settings such way that you can upload any objects.
Screenshot 2024-08-19 at 14.25.23
Go to the "Stages" section, select the PUT method, and copy the "Invoke URL".
Screenshot 2024-08-19 at 14.42.00
Open Postman, select "PUT" as the method, paste the URL, and replace {bucket} with your S3 bucket name and {filename} with the file you want to upload. In Postman, select "Body", choose "Binary", and upload the file.
Screenshot 2024-08-19 at 14.58.23
Check your S3 bucket to confirm that the file was successfully uploaded.
Screenshot 2024-08-19 at 15.02.41

Conclusion

Uploading files to S3 with Amazon API Gateway provides a quick, safe, and adaptable solution to upload data to a variety of applications. You can easily set up your S3 bucket and API Gateway to handle file uploads by following this step-by-step tutorial.

この記事をシェアする

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.